package entity

import (
	"github.com/jinzhu/gorm"
)

type FriendLink struct {
	*gorm.Model
	Title     string `json:"title"`
	Des       string `json:"des"`
	AvatarUrl string `json:"avatar_url"`
	BlogUrl   string `json:"blog_url"`
}

func (fl FriendLink) GetByPagination(db *gorm.DB, offset int, pageSize int) (error, []FriendLink) {
	var list []FriendLink
	err := db.Limit(pageSize).Offset(offset).Order("created_at").Find(&list).Error
	if gorm.ErrRecordNotFound == err {
		return nil, list
	} else {
		return err, list
	}
}

func (fl FriendLink) Create(db *gorm.DB) error {
	return db.Create(&fl).Error
}

func (fl FriendLink) Count(db *gorm.DB) (error, uint) {
	var cnt uint
	err := db.Model(&fl).Count(&cnt).Error
	return err, cnt
}

func (fl FriendLink) Update(db *gorm.DB) error {
	return db.Save(&fl).Error
}

func (fl FriendLink) Delete(db *gorm.DB) error {
	return db.Delete(&fl).Error
}

func (fl FriendLink) GetFriendLinkListBySearchText(db *gorm.DB, searchText string) ([]FriendLink, error) {
	var friendLinks []FriendLink
	err := db.Where("title like ?", "%"+searchText+"%").Find(&friendLinks).Error
	return friendLinks, err
}
